﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace shp.Web.Controllers.FileServ
{
    /// <summary> 文件传输API </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FileTransferController : ControllerBase
    {


        /// <summary> 文件存放目录 </summary>
        internal static string _fileDir = @"D:\guantong\webfiles";


        static FileTransferController()
        {
            if (!Directory.Exists(_fileDir))
            {
                new DirectoryInfo(_fileDir).Create();
            }
        }

        //public FileTransferController(IWebHostEnvironment webHostEnvironment)
        //{
        //    _fileDir = Path.Combine(webHostEnvironment.ContentRootPath, "fileDir");
        //    if (!Directory.Exists(_fileDir))
        //    {
        //        Directory.CreateDirectory(_fileDir);
        //    }
        //}


        /// <summary> 上传 </summary>
        [HttpPost]
        [RequestSizeLimit(1000_000_000)]
        public async Task<bool> Upload([Required] IFormFile file)
        {

            var filePath = Path.Combine(_fileDir, file.FileName);
            using (var fs = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(fs);
                await fs.FlushAsync();
            }

            return true;



            /*
             * 不管用 
             * 
             * 遇到大文件不能上传的问题, 需要修改iis上的配置
            在web.config中加入如下配置：
            代码如下:
            <system.webServer>
                <security>
                    <requestFiltering>
                        <requestLimits maxAllowedContentLength="10485760000"></requestLimits>
                    </requestFiltering>
                </security>
            </system.webServer>
            
            单位是 byte
             * 
             */

        }



        /// <summary> 上传 到指定文件夹 </summary>
        [HttpPost]
        [RequestSizeLimit(1000_000_000)]
        public async Task<bool> UploadToDir([Required] IFormFile file,[FromQuery] string dir)
        {
            var fi = new DirectoryInfo(Path.Combine(_fileDir, dir));
            if (!fi.Exists)
            {
                fi.Create();
            }


            var filePath = Path.Combine(_fileDir, dir, file.FileName);
            using (var fs = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(fs);
                await fs.FlushAsync();
            }

            return true;
        }



        /// <summary> 检查是否存在 </summary>
        [HttpGet]
        public bool IsExists(string name)
        {
            var filePath = Path.Combine(_fileDir, name);
            return System.IO.File.Exists(filePath);
        }


        /// <summary> 下载 </summary>
        [HttpGet]
        public FileStreamResult Download(string fileName)
        {
            var filePath = Path.Combine(_fileDir, fileName.Trim('\\'));

            if (!System.IO.File.Exists(filePath))
            {
                throw new FileNotFoundException();
            }

            var fsr = new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream");
            fsr.FileDownloadName = Path.GetFileName(fileName);    //指定文件名称

            return fsr;
        }



    }




}
